package com.example.e.Fragment

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.e.Adapter.OrderHistoryAdapter
import com.example.e.Bean.Order
import com.example.e.R
import kotlinx.android.synthetic.main.activity_location.*

import kotlinx.android.synthetic.main.fragment_order.*
import java.io.*

class OrderFragment : Fragment(){

    private val history_order_data = ArrayList<Order>()
    private val file_name3 = "order_history_data"
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_order, container, false)
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val layoutManager = LinearLayoutManager(activity)
        recyclerviewOrder.layoutManager = layoutManager

        load()
        //history_order_data.add()
        refreshOrder()

    }

    fun load(){
        try {
            val input = activity?.openFileInput(file_name3)
            val reader = BufferedReader(InputStreamReader(input))
            reader.use {
                reader.forEachLine {
                    val info = parseOrder(it)
                    history_order_data.add(info)
                }
            }
        }catch (e: IOException){
            e.printStackTrace()
        }
    }
    fun parseOrder(order_str:String):Order{
        val str = order_str.split(",")
//        val shouhuo_name = address_str.substring(0,splitIndex).trim()
//        val shouhuo_number = address_str.substring(splitIndex+1,splitIndex).trim()
        val order_count = str[0]
        val order_price = str[1]
        return Order(order_count,order_price)
    }
    fun refreshOrder(){
        val adapter = OrderHistoryAdapter(history_order_data)
        recyclerviewOrder.adapter = adapter
    }



}